import pyglet,sys,os,glob
from pyglet.window import key
class ProtoClass:
    def __init__(self,width,height):
        self.window = pyglet.window.Window(width,height,resizable=True)
        self.piclist = [f for f in glob.glob(os.path.join(sys.argv[1],'*.*')) if
                        f[-3:].lower() == 'jpg' or f[-3:].lower() == 'png' or
                        f[-3:].lower() == 'gif' or f[-3:].lower() == 'bmp' or
                        f[-4:].lower() == 'jpeg']
        if not len(self.piclist): raise SystemExit()
        self.picix = 0
        self.sprite = None
        self.loadpic()
        self.recalc = False
    def loadpic(self):
        if self.piclist[self.picix][-3:] == 'gif':
            image = pyglet.image.load_animation(self.piclist[self.picix])
        else: image = pyglet.image.load(self.piclist[self.picix])
        self.sprite = pyglet.sprite.Sprite(image)
        self.window.set_caption(os.path.split(self.piclist[self.picix])[1])
        self.recalcpos()
    def recalcpos(self):
        self.sprite.scale = 1.0
        self.sprite.scale = min(self.window.width/float(self.sprite.width),
                                self.window.height/float(self.sprite.height))
        self.sprite.x = (self.window.width - self.sprite.width)/2
        self.sprite.y = (self.window.height - self.sprite.height)/2

ptype = ProtoClass(800,600)

@ptype.window.event
def on_key_press(symbol,modifiers):
    if symbol == key.LEFT:
        ptype.picix = (ptype.picix - 1) % len(ptype.piclist)
        ptype.loadpic()
    if symbol == key.RIGHT:
        ptype.picix = (ptype.picix + 1) % len(ptype.piclist)
        ptype.loadpic()
@ptype.window.event
def on_draw():
    if ptype.recalc: ptype.recalcpos()
    ptype.window.clear()
    ptype.sprite.draw()
@ptype.window.event
def on_resize(width,height):
    ptype.recalc = True
pyglet.app.run()
